package com.jluzh10.learning.service.Impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jluzh10.learning.dao.CourseGroupMapper;
import com.jluzh10.learning.domain.CourseGroup;
import com.jluzh10.learning.domain.CourseGroupExample;
import com.jluzh10.learning.service.CourseGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author yzg
 * @create 2019/5/25
 */
@Service
@Transactional
public class CourseGroupServiceImpl implements CourseGroupService {
    private final CourseGroupMapper groupMapper;

    @Autowired
    public CourseGroupServiceImpl(CourseGroupMapper groupMapper) {
        this.groupMapper = groupMapper;
    }

    /**
     * 添加一个课程分组
     * @param group
     * @return
     */
    @Override
    public int add(CourseGroup group) {

        return groupMapper.insertSelective(group);
    }

    /**
     * 查询所有有效的课程分组
     * @return
     */
    @Override
    public List<CourseGroup> findAll(Integer num) {

        CourseGroupExample groupExample=new CourseGroupExample();
        //按照权重排序
        groupExample.setOrderByClause("weight desc");
        //筛选有效状态
        groupExample.createCriteria().andStateEqualTo((byte)1);

        if (num!=null&&num!=0){
            //分页
            PageHelper.startPage(1,num);
        }
        return groupMapper.selectByExample(groupExample);
    }

}
